#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef double db;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
const int MAXN=2e3+10;
int n,v[MAXN],f[MAXN][MAXN];
int main()
{
	n=read();
	for(int i=1;i<=n;++i)v[i]=read(),f[i][i]=v[i]*n;
	for(int i=2;i<=n;++i)
		for(int l=1;l+i-1<=n;++l)
		{
			int r=l+i-1;
			f[l][r]=max(f[l][r-1]+v[r]*(n-i+1),f[l+1][r]+v[l]*(n-i+1));
		}
	printf("%d\n",f[1][n]);
	return 0;
}
